package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.KhjcRewardPunishmentCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictSelectResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.KhjcRewardPunishmentResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.KhjcRewardPunishmentUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxRewardPunishmentEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface KhjcRewardPunishmentConvert {
    KhjcRewardPunishmentConvert INSTANCE = Mappers.getMapper(KhjcRewardPunishmentConvert.class);

    KhjcRewardPunishmentResultDTO convertTo(JzdxRewardPunishmentEntity s);

    default KhjcRewardPunishmentResultDTO convert(JzdxRewardPunishmentEntity s, List<DictSelectResultDTO> dctCategoryList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(dctCategoryList)) {
            return null;
        }

        KhjcRewardPunishmentResultDTO result = convertTo(s);
        String parent = dctCategoryList.stream().filter(category -> Objects.equals(category.getCode(), s.getJclb())).map(DictSelectResultDTO::getPcode).findFirst().orElse(null);

        List<String> categoryList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(parent), () -> categoryList.add(parent));
        categoryList.add(s.getJclb());

        result.setCategoryList(categoryList);
        return result;
    }

    JzdxRewardPunishmentEntity createTo(KhjcRewardPunishmentCreateDTO s);

    default JzdxRewardPunishmentEntity create(KhjcRewardPunishmentCreateDTO s, Long tenantId) {
        JzdxRewardPunishmentEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        entity.setLcbh(CodeUtils.entrustCode());
        entity.setJclb(s.getCategoryList().get(s.getCategoryList().size() - 1));
        return entity;
    }

    JzdxRewardPunishmentEntity updateTo(KhjcRewardPunishmentUpdateDTO s);

    default JzdxRewardPunishmentEntity update(KhjcRewardPunishmentUpdateDTO s, Long tenantId) {
        JzdxRewardPunishmentEntity entity = updateTo(s);
        entity.setTenantId(tenantId);
        entity.setJclb(s.getCategoryList().get(s.getCategoryList().size() - 1));
        return entity;
    }
}
